<%@ page import="java.util.*,
                 com.actimind.actitime.util.*,
                 com.actimind.actitime.beans.*,
                 com.actimind.actitime.*,
                 org.apache.struts.util.ResponseUtils,
                 com.actimind.actitime.user.NNCommentForm,
                 org.apache.struts.Globals"%>
<%@ page errorPage="/error.jsp" %>
<%@ taglib uri="/WEB-INF/at.tld" prefix="at"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<at:CheckLoggedIn/>
<bean:define id="form" scope="session" name="SubmitTTForm" type="com.actimind.actitime.user.SubmitTTForm"/>
<bean:define id="calendar" name="form" property="calPrefs" type="com.actimind.actitime.beans.CalendarPrefsBean"/>
<bean:define id="user" name="<%=Keys.LOGGEDIN_USER_KEY%>" type="UserBean" scope="session"/>

<html>
<head>
    <title><bean:write name="title-prefix" scope="application"/> Submit Time-Track</title>
    <script language="JavaScript">
        // array of modified objects
        var dataModified = new Array();
        // current field value
        var currentValue = null;
        var calLayout = new Array(<%for(int i = 0; i < 7; i++) {%>"<%=calendar.getPrintableDayOfWeek_FullName(i)%>"<%if(i != 6) {%>,<%}}%>);

    </script>
    <script language="JavaScript" src="../js/at.js"></script>
    <script language="JavaScript" src="../js/submit_tt.js"></script>
    <script language="JavaScript" src="../js/calendar.js?r=1"></script>
    <script language="JavaScript" src="../js/showstick.js"></script>
    <link rel="stylesheet" href="../css/calendar_small.css" type="text/css">
    <link rel="stylesheet" href="../css/at.css" type="text/css">

<script language="JavaScript">
var addTasksPopup = null;


var exitConfirmMessage = "You modified your time-track information but did not save it.\nIf you leave the page, all your modifications will be lost.\n\n" +
                         "Click \"Ok\" to discard your changes.\nClick \"Cancel\" to remain on \"Submit Time-Track\" page."


function openAddTasksPopup()
{
    modifiedCount = 0;
    for(var k in dataModified)
        modifiedCount++;
    if(modifiedCount != 0)
        document.SubmitTTForm.dataModified.value = 1;

    addTasksPopup = popup('<html:rewrite href="add_tasks.do?shownCustomer=&shownProject="/>', 'popupAddTasks', 770, 548, false, false, true);
}

function selectDate(dateStr)
{
    if(isModified())
    {
        if(!confirm(exitConfirmMessage)) return;
    }
    var date = strToDate(dateStr);
    location.href = '<html:rewrite href='submit_tt.do?dateStr='/>' + date.getFullYear() + makeStr(date.getMonth() + 1) + makeStr(date.getDate());
}

setExitHandler(exitConfirmMessage, "isModified()");
</script>
</head>
<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0"
    onLoad="document.forms[0].autocomplete='off'; recalculateWeek(document.SubmitTTForm); initDHTMLAPI(); recalculateTotalOvertime(document.SubmitTTForm); showExclamationMarks();"
    onUnload="if(nnwindow && !nnwindow.closed) nnwindow.close(); if(addTasksPopup && !addTasksPopup.closed) addTasksPopup.close(); ">

<jsp:include page="../top.jsp" flush="false">
    <jsp:param name="pageSection" value="submit_tt"/>
</jsp:include>

<html:form action="/user/submit_tt" method="post">
<html:hidden property="submitted" value="true"/>
<html:hidden property="dateStr"/>
<html:hidden property="deleteTimeTrack" value="0"/>
<html:hidden property="pageAction" value=""/>
<html:hidden property="addTaskList" value=""/>
<html:hidden property="dataModified"/>

<table width="800" align="center" cellspacing="0" cellpadding="0" border="0">
<tr>
    <td colspan="2">
        <span class="pagetitle">Submit Time-Track</span>
        <br>
        <at:errors/>
        <br>
        <span class="textemphasis" style="font-size: 12pt"><%=form.getPrintableWeekStart()%> - <%=form.getPrintableWeekEnd()%></span>
        <br>
    </td>
    <td width="120" align="right" rowspan="2">
        <table width="100%">
        <tr>
            <%
                Calendar cal = new GregorianCalendar();
                cal.setTime(form.getDate());
                cal.add(Calendar.MONTH, -1);
                String prevMonth = "/user/submit_tt.do?dateStr=" + 
                    TimeUtils.toString(cal.getTime(), "yyyyMMdd");
                cal.add(Calendar.MONTH, 2);
                String nextMonth = "/user/submit_tt.do?dateStr=" + 
                    TimeUtils.toString(cal.getTime(), "yyyyMMdd");
            %>
            <td><html:link page="<%=prevMonth%>">&lt;&lt;</html:link></td>
            <td class="text" align="center"><span style="font-weight: bold"><%= TimeUtils.toString(form.getDate(), "MMM, yyyy") %></span></td>
            <td><html:link page="<%=nextMonth%>">&gt;&gt;</html:link></td>
        </tr>
        </table>
<script language="JavaScript">
  var iDays = new Array(<%=Util.getDateSetAsCSV(calendar.getInvertedDays())%>);
  printLayoutCalendar('<%= TimeUtils.toString(form.getDate(), "yyyy-MM-dd") %>',
  '<%= TimeUtils.todayString()%>', '<%= TimeUtils.toString(form.getWeekStart(), "yyyy-MM-dd") %>',
  '<%= TimeUtils.toString(form.getWeekEnd(), "yyyy-MM-dd") %>', 'selectDate', '../img/pixel.gif', '<%=calendar.getCalendarLayout()%>', iDays);
</script>
    </td>
</tr>
<tr>
    <td valign="center">
        <html:link href="javascript: openAddTasksPopup(); "><img border="0" src="../img/add_tasks.gif"/></html:link>
    </td>
    <td width="100%" valign="center">
        <a href="javascript: openAddTasksPopup();">
        Add tasks to the list
        </a>
    </td>
</tr>
</table>

<br>

<table width="800" align="center" cellspacing="1" cellpadding="3" border="0">
<tr>
    <td width="50" class="listtblcolheader" rowspan="2">Deadline</td>
    <td width="50" class="listtblcolheader" rowspan="2">Project</td>
    <td width="220" class="listtblcolheader" rowspan="2">Task</td>
    <td class="listtblcolheader" align="center" colspan="7">Spent (hh:mm)</td>
    <td width="20" class="listtblcolheader" align="center" rowspan="2">Del<br>Row</td>
</tr>
<tr>
<%
   for(int day = 0; day <= 6; day++)
   {
     
     String className = (form.isHoliday(day)) ? "calendarWeekendDayHeaderNormal" : "calendarWorkingDayHeaderNormal";
%>
    <td align="center" class="<%=className%>"><%=calendar.getPrintableDayOfWeek(day)%><br><%=form.getPrintableDayOfMonth(day)%></td>
<% } %>
</tr>

<logic:equal name="form" property="tasks.empty" value="true">
<tr>
    <td align="center" class="formbuttonpane" colspan="11">
        &nbsp;
        <br>
        <span class="text">To submit time-track you should select tasks you are working on.</span>
        <br>
        <span class="text">To add tasks to the list click the link above the table.</span> 
        <br>
        &nbsp;
    </td>
</tr>
</logic:equal>

<logic:equal name="form" property="tasks.empty" value="false">
<logic:iterate id="task" name="form" property="tasks" type="TaskBean">
<tr>
    <td width="50" class="listtblcell">
        <logic:notEmpty name="task" property="deadlineDate"><%=task.getPrintableDeadlineDate()%></logic:notEmpty>
        <logic:empty name="task" property="deadlineDate">&nbsp;</logic:empty>
    </td>
    <td width="100" class="listtblcell">
        <%=ResponseUtils.filter( StringUtils.trimWithDots( task.getProject().getName(), 50 ) )%>
    </td>
    <td class="listtblcell">
        <table width="100%" cellspacing="0" cellpadding="0">
        <tr>
            <td align="left" class="listtblcell"><%=task.getName()%></td>
            <td align="right"><a title='Click to view task' href="javascript:popup('<html:rewrite page='<%="/tasks/task_details.do?popupMode=true&taskId=" + task.getId()%>'/>', 'popup', 810, 600, false, false, false);"><img border="0" align="absmiddle" src="../img/note.gif"></a></td>
        <tr>
        </table>
    </td>
    <bean:define id="weekInfo" name="form" property='<%="timeTrack["+task.getId()+"]"%>' type="com.actimind.actitime.user.WeekTTInfo"/>

<%
   for(int day = 0; day <= 6; day++)
   {
     String className = form.isToday(day) ? "calendarCurrentDayNormal" :
        (form.isHoliday(day)) ? "calendarWeekendDayNormal" : "calendarWorkingDayNormal";

     int taskID = task.getId();
     String spentProp = "timeTrack["+taskID+"].spentStr["+day+"]";
     String onchange = "return checkSpent(" + day + ", this, event);";
     String onfocus  = "return focusSpent(" + day + ", this);";
     String commentHidden = "timeTrack["+taskID+"].comment[" + day + "]";
     String hilightObjId = "timeTrack[" + taskID + "].obj[" + day + "]";
%>
    <td align="center" class="<%=className%>" nowrap id="<%=hilightObjId%>" valign="middle">

    <% if(!form.isFutureDay(day)) { %>
        <%
            String commentURL = response.encodeURL("ttr_comment.do?taskId=" + taskID +
                    "&dateStr=" + form.getDateByWeekDay(day));
         %>
        <logic:equal name="task" property="completed" value="true">
            <html:hidden name="form" property="<%=spentProp%>"/>
            <input type="hidden" name="<%=commentHidden%>" value="<%=StringUtils.encodeQuoteCharacter(form.getTimeTrack(taskID).getComment(day))%>">
            <table width="100%" cellspacing="0" cellpadding="0">
              <tr>
                    <td align="left"><logic:notEqual name="form" property="<%=spentProp%>" value=""><span class="text">&nbsp;<bean:write name="form" property="<%=spentProp%>"/></span></logic:notEqual></td>
                    <td align="right"><logic:equal value="true" property='<%="commentAvailable[" + day + "]"%>' name="weekInfo"><a title='Click to view comments' href="javascript:void(0);" onclick="return viewCommentWindow(this, <%=taskID%>, <%=day%>, '<%=commentHidden%>', '<%=commentURL%>');"><img src="../img/comment_entered.gif" border="0" align="absmiddle" hspace="2"></a></logic:equal></td>
              </tr>
            </table>
        </logic:equal>
        <logic:equal name="task" property="completed" value="false">
            <html:hidden name="form" property='<%=commentHidden%>'/>
            <%
                 String imageName = "timeTrack[" + taskID + "].commentImg[" + day + "]";
            %>
            <table width="100%" cellspacing="0" cellpadding="0">
              <tr>
                    <td align="left"><html:text style="font-family: monospace" styleClass="text" property="<%=spentProp%>" size="5" maxlength="5" onchange="<%=onchange%>" onfocus="<%=onfocus%>"/></td>
                    <td align="right"><a title='Click to edit comments' href="javascript:void(0);" onclick="return editCommentWindow(this, <%=taskID%>, <%=day%>, '<%=commentHidden%>', '<%=commentURL%>');"><logic:equal value="false" property='<%="commentAvailable[" + day + "]"%>' name="weekInfo"><img hspace="2" vspace="6" align="absmiddle" src="../img/comment_blank.gif" border="0" name="<%=imageName%>"></logic:equal><logic:equal value="true" property='<%="commentAvailable[" + day + "]"%>' name="weekInfo"><img hspace="2" align="absmiddle" src="../img/comment_entered.gif" border="0" name="<%=imageName%>"/></logic:equal></a></td>
              </tr>
            </table>
        </logic:equal>
    <% } else { %>
        &nbsp;
    <% } %>
    </td>
<% } %>
    <td class="listtblcell" align="center" valign="center">
        <logic:equal name="task" property="completed" value="false">
          <a title="Click to remove this task from the list" href="javascript: deleteTimeTrack(<%=task.getId()%>);"><img border="0" src="../img/delete_tt.gif"></a>
        </logic:equal>
        <logic:notEqual name="task" property="completed" value="false">
            &nbsp;
        </logic:notEqual>
    </td>
</tr>
</logic:iterate>
<tr>
    <td class="listtblcolheader" align="right" colspan="3">
        Week Total: <input style="font-family: monospace" class="text" type="text" disabled="true" name="weekTotal" size="6" onchange="recalculateWeek(this.form);">
    </td>
<% for(int day = 0; day <= 6; day++)
   {
     
     String className = form.isHoliday(day) ? "calendarWeekendDayHeaderNormal" : "calendarWorkingDayHeaderNormal";
%>
   <% if(!form.isFutureDay(day)) { %>
    <td align="left" class="<%=className%>">
        <input style="font-family: monospace" class="text" type="text" disabled="true" name="dayTotal[<%=day%>]" size="5" onchange="recalculateDay(this.form, <%=day%>);">
    </td>
   <% } else  { %>
    <td align="left" class="<%=className%>">
        <img src="../img/pixel.gif" width="40" height="1">
    </td>
   <% } %>
<% } %>
    <td class="listtblcolheader">&nbsp;</td>
</tr>
<logic:equal name="user" property="overtimeTrackingEnabled" value="true">
<tr>
   <td colspan="3"><span class="text">Click to calculate overtime automatically: </span></td>
<%
   int userWorkdayDuration = (user.getWorkdayDuration() > 0) ? user.getWorkdayDuration() : calendar.getDefaultWorkdayDuration();
   for(int day = 0; day <= 6; day++)
   {
              String printableDayOfWeek = calendar.getPrintableDayOfWeek_FullName(day);
%>
   <% if(!form.isFutureDay(day)) { %>
    <td align="left">&nbsp;<a title="Click to calculate overtime for <%=printableDayOfWeek%>" href="javascript:calcOvertime(<%=day%>);"><img src="../img/overtime_button.gif" border="0" hspace="4"></a>
    </td>
   <% } else  { %>
    <td align="left">
        <img src="../img/pixel.gif" width="40" height="1">
    </td>
   <% } %>
<% } %>
</tr>
<tr>
    <td class="overtimeheader" align="right" colspan="3">
        Overtime: <input style="font-family: monospace" class="text" type="text" disabled="true" name="overtimeTotal" size="6" onchange="recalculateTotalOvertime(this.form);">
    </td>

<% for(int day = 0; day <= 6; day++)
   {
       int workDayDuration = form.isHoliday(day) ? 0 : userWorkdayDuration;
       String dayTypeHidden = "dayType[" + day + "]";
%>
   <% if(!form.isFutureDay(day)) { %>
    <td align="left" class="overtimeheader">
        <table cellspacing="0" cellpadding="0" width="100%">
            <tr align="left">
                <td align="left"><input type="hidden" name="<%=dayTypeHidden%>" value="<%=workDayDuration%>"/><input style="font-family: monospace" class="text" type="text" name="overtime[<%=day%>]" size="5" onchange="return checkOvertime(this, <%=day%>);" value="<%=form.getOvertime(day)%>"></td>
                <td valign="middle" align="right"><img src="../img/pixel.gif" width="12" height="12" name="overtimeImg[<%=day%>]"></td>
            </tr>
        </table>
    </td>
   <% } else  { %>
    <td align="left" class="overtimeheader">
        <img src="../img/pixel.gif" width="40" height="1">
    </td>
   <% } %>
<% } %>
    <td class="overtimeheader">&nbsp;</td>
</tr>
</logic:equal>
<tr><td colspan="11"><img src="../img/pixel.gif" height="4" width="1"></td></tr>
<tr>
    <td colspan="11" align="center" class="formbuttonpane">
        <input type="button" value="Submit Time-Track"
            onclick="SubmitTimeTrack(event);"/>
    </td>
</tr>
<%  String notes = "";
    if(form.isFutureDay(6) && user.isOvertimeTrackingEnabled())
        notes = "Notes:";
   else
     if(form.isFutureDay(6) || user.isOvertimeTrackingEnabled())
        notes = "Note:";

     int userWorkdayDuration = (user.getWorkdayDuration() > 0) ? user.getWorkdayDuration() : calendar.getDefaultWorkdayDuration();
     String hours = new Integer(userWorkdayDuration / 60).toString();
     Integer mins = new Integer(userWorkdayDuration % 60);
     String minutes = (mins.intValue() < 10) ? "0" + mins.toString() : mins.toString();

    if(!"".equals(notes)) {
%>
<tr>
    <td valign="top" colspan="11">
        <table border="0">
            <tr>
                <td valign="top"><span class="notehead"><%=notes%></span></td>
                <td valign="top">
                   <% if(form.isFutureDay(6)) { %>
                        <span class="smalltext">You cannot submit time-track for the future.<br></span>
                   <% } %>
                   <% if(user.isOvertimeTrackingEnabled()) { %>
                        <span class="smalltext">Overtime is marked with <img src="../img/exclamation.gif" width="12" height="12"> if it differs from the automatically calculated value: (Day Total - <%=hours%>:<%=minutes%>) for working days; (Day Total) for nonworking days. </span>
                   <% } %>
                </td>
            </tr>
        </table>
    </td>
</tr>
<% } %>
</logic:equal>
</table>
</html:form>
<div id="editDiv" style="position:absolute; visibility:hidden; z-index:1000;width:330px;height:150px;">
 <form name="savecom" onreset="return false;">
  <table cellspacing="0" cellpadding="0" border="0" align="left" width="100%">
    <tr align="left" width="100%">
      <td align="left"><img src="../img/1-1.gif" width="23" height="22"></td>
      <td bgcolor="#6b9eff" class="topborder" nowrap>
        <span class="whitetitle">Edit Time-Track Comments</span>
      </td>
      <td align="right" nowrap>
        <a href="javascript:void(0);" onclick="HideWindow();"><img src="../img/1-2.gif" border="0" title="Click here to close"/></a><br>
      </td>
    </tr>
    <tr bgcolor="#6b9eff" align="left">
     <td class="leftborder">&nbsp;</td>
     <td><textarea name="comment" rows="5" cols="32" wrap="virtual"></textarea></td>
     <td class="rightborder">&nbsp;</td>
    </tr>
    <tr><td class="leftborder" bgcolor="#6b9eff">&nbsp;</td>
     <td rowspan="2" bgcolor="#6b9eff" width="100%" class="bottomborder" valign="middle" align="center">
       <table cellspacing="4" cellpadding="0" border="0" width="100%">
         <tr>
          <td align="left">
            <input type="button" class="infobutton" width="48" value="OK" id="scbutton" name="nscbutton" onclick="SaveComment();">
          </td>
          <td align="right">
           <input type="button" class="infobutton" value="Cancel" onclick="HideWindow();" name="nccbutton">
          </td>
         </tr>
       </table>
     </td>
     <td bgcolor="#6b9eff" class="rightborder">&nbsp;</td>
    </tr>
    <tr align="left">
     <td align="left"><img src="../img/2-1.gif"></td>
     <td align="right"><img src="../img/2-2.gif"></td>
    </tr>
 </table>
  </form>
</div>
<div id="infoDiv" style="position:absolute;visibility:hidden;z-index:1001;width:330px;height:150px;">
  <table cellspacing="0" cellpadding="0" border="0" align="left" width="100%">
    <tr align="left" width="100%">
      <td align="left"><img src="../img/1-1.gif" width="23" height="22" ></td>
      <td bgcolor="#6b9eff" class="topborder" align="left" width="100%" nowrap="">
        <span class="whitetitle">View Time-Track Comments</span>
      </td>
      <td align="right" nowrap>
        <a href="javascript:void(0);" onclick="HideWindow();"><img src="../img/1-2.gif" border="0" title="Click here to close"/></a><br>
      </td>
    </tr>
    <tr bgcolor="#6b9eff" align="left" >
     <td class="leftborder">&nbsp;</td>
     <td><span class="white" id="infoText"></span></td>
     <td class="rightborder">&nbsp;</td>
    </tr>
    <tr align="left">
     <td align="left"><img src="../img/2-1.gif"></td>
     <td bgcolor="#6b9eff" width="100%" class="bottomborder">&nbsp;</td>
     <td align="right"><img src="../img/2-2.gif"></td>
    </tr>
 </table>
</div>
<br>
<br>
</body>
</html>
